package com.chapter16.Homework;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class Homework02 {
    public static void main(String[] args) {
        Card card = new Card();
        Thread thread = new Thread(card);
        Thread thread1 = new Thread(card);
        thread1.start();
        thread.start();
    }
}

class Card implements Runnable {
    private static int balance = 10000;
    private boolean loop = true;

    @Override
    public void run() {
        while (loop) {
            put();
        }
    }

    public synchronized void put() {
            if (balance < 1000) {
                loop = false;
                return;
            }
            balance -= 1000;
            System.out.println(Thread.currentThread().getName() + "余额" + balance);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
}
